//
//  AlbumHelper.m
//  album
//
//  Created by 云天 李 on 12-1-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AlbumHelper.h"

@implementation AlbumHelper

// 创建随机图片名
+ (NSString *)createRandomPictureName
{
    // Just a simple implementation of a random number between 899999999999 and 999999999999
	return [NSString stringWithFormat:@"%u", arc4random() % (999999999999 - 899999999999) + 899999999999];
}

// 图片文件路径
+ (NSString *)pictureFilePath: (NSString *)pictureName withAlbum:(NSUInteger)albumid
{    
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *albumPath = [NSString stringWithFormat:@"%d", albumid];
    albumPath = [documentsDirectory stringByAppendingPathComponent:albumPath];
    
    // 创建相册目录
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath: albumPath])
    {
        [fileManager createDirectoryAtPath:albumPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    
    // 以扩展名为jpg返回图片的完成路径
    NSString *pic = [[albumPath stringByAppendingPathComponent: pictureName] stringByAppendingString:@".jpg"];
    
	NSLog(@"path: %@", pic);
	
	return pic;
}

//  保存图片
+ (void) savePictureData: (NSData *)data withName: (NSString *)pictureName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    [fileManager createFileAtPath:pictureName contents:data attributes:nil];
}

// 创建缩略图
+ (UIImage *)createThumbnail: (UIImage *) image
{
    float thumbnailWidth = kThumbnailDimension;
    float thumbnailHeight = kThumbnailDimension;
    
    CGSize itemSize = CGSizeMake(thumbnailWidth, thumbnailHeight);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [image drawInRect:imageRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return thumbnail;
}

@end
